package com.polaris.live.adapter.gift

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.polaris.live.R
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.AdapterGiftWallBackpackBinding
import com.polaris.live.databinding.ItemGiftWallBackpackToolBinding
import com.polaris.live.resp.back_resp.UserBackpackGiftResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.image.loadGiftImage
import com.polaris.live.widget.refresh.BaseBindingViewHolder
import java.util.concurrent.TimeUnit

/**
 * GiftWallBackPackAdapter
 *
 * @author Created by 半仙 on 2023/9/18/018 18:48
 */
class GiftWallBackPackAdapter :
    BaseQuickAdapter<UserBackpackGiftResp, BaseWallBackPackHolder<*, UserBackpackGiftResp>>(0) {

    private var mSelectPosition = -1
    private var mSelectTimes = 1

    override fun getDefItemViewType(position: Int): Int {
        return data[position].backpackType.toInt()
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseWallBackPackHolder<*, UserBackpackGiftResp> {
        return when (viewType) {
            2 -> {
                GiftViewHolder(
                    AdapterGiftWallBackpackBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            3 -> {
                GiftViewHolder2(
                    ItemGiftWallBackpackToolBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            else -> {
                GiftViewHolder(
                    AdapterGiftWallBackpackBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }
        }
    }

    override fun convert(holder: BaseWallBackPackHolder<*, UserBackpackGiftResp>, item: UserBackpackGiftResp) {
        holder.mSelectPosition = mSelectPosition
        holder.mSelectTimes = mSelectTimes

        holder.bind(item)
    }

    fun updateSelected(position: Int, times: Int) {
        mSelectPosition = position
        mSelectTimes = times
    }

    class GiftViewHolder(mBinding: AdapterGiftWallBackpackBinding) :
        BaseWallBackPackHolder<AdapterGiftWallBackpackBinding, UserBackpackGiftResp>(mBinding) {

        override fun bind(item: UserBackpackGiftResp) {
            mBinding.apply {
                val data = item.getGiftBean()

                val position = absoluteAdapterPosition

                if (position == mSelectPosition) {
                    giftWallSelectNum.visibility = View.VISIBLE
                    relGiftWallBack.setBackgroundResource(R.drawable.r8_with_fe1849)
                } else {
                    giftWallSelectNum.visibility = View.GONE
                    relGiftWallBack.background = null
                }
                giftWallSelectNum.text = "$mSelectTimes"

                giftWallName.text = "${data?.giftName} x${item.amount}"

                if (!item.expiredTime.isNullOrEmpty()) {
                    timeLl.visibility = View.VISIBLE
                    val time = item.expiredTime.toLong() - CacheUtil.getServerTime()
                    val string = formatTime(time)
                    timeTv.text = string
                } else {
                    timeTv.text = StringUtils.getString(string.permanent)
                }

                Glide.with(giftWallImg.context)
                    .loadGiftImage(data?.pic)
                    .into(giftWallImg)

                if (data?.type == LiveConst.GiftType.LUCK_GIFT) {
                    ivLuck.visibility = View.VISIBLE
                } else {
                    ivLuck.visibility = View.GONE
                }
            }
        }

    }

    class GiftViewHolder2(mBinding: ItemGiftWallBackpackToolBinding) :
        BaseWallBackPackHolder<ItemGiftWallBackpackToolBinding, UserBackpackGiftResp>(mBinding) {

        override fun bind(item: UserBackpackGiftResp) {
            mBinding.apply {
                val data = item.getUserBackpackConsumableResp()
                relGiftWallBack.background = null
                giftWallName.text = "${data?.name}"

                if (!item.expiredTime.isNullOrEmpty()) {
                    timeLl.visibility = View.VISIBLE
                    val time = item.expiredTime.toLong() - CacheUtil.getServerTime()
                    val string = formatTime(time)
                    timeTv.text = string
                } else {
                    timeLl.visibility = View.INVISIBLE
                    timeTv.text = StringUtils.getString(string.permanent)
                }

                Glide.with(giftWallImg.context)
                    .loadGiftImage(data?.displayUrl)
                    .into(giftWallImg)
                if (item.amount > 1) {
                    countTv.visibility = View.VISIBLE
                    countTv.text = "x${item.amount}"
                } else {
                    countTv.visibility = View.GONE
                }
            }
        }

    }
}

abstract class BaseWallBackPackHolder<VB : ViewBinding, T>(binding: VB) :
    BaseBindingViewHolder<VB, T>(binding) {

    var mSelectPosition = -1
    var mSelectTimes = 1
    fun formatTime(timeInMillis: Long): String {
        val days = TimeUnit.MILLISECONDS.toDays(timeInMillis)
        val hours = TimeUnit.MILLISECONDS.toHours(timeInMillis)
        val minutes = TimeUnit.MILLISECONDS.toMinutes(timeInMillis)
        val seconds = TimeUnit.MILLISECONDS.toSeconds(timeInMillis)
        return when {
            days > 0 -> StringUtils.getString(string.days, days)
            hours > 0 -> StringUtils.getString(string.hours, hours)
            minutes > 0 -> StringUtils.getString(string.minutes, minutes)
            seconds > 0 -> StringUtils.getString(string.seconds, seconds)
            else -> StringUtils.getString(string.expired)
        }
    }
}